﻿using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtJudge.DataEngine.Model.ElmentModel;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace LdtJudge.DataEngine.Common
{
    /// <summary>
    ///节点对象生成节点xml
    /// </summary>
    public class ModelToXml
    {
        public ModelToXml()
        {
        }
        /// <summary>
        /// 进行生成xml文件返回对象
        /// </summary>
        /// <param name="nodeModels">要素所有信息</param>
        /// <param name="sys_CaseCause">案由对象</param>
        /// <returns></returns>
        public XmlElement BuildXmlNode(List<NodeModel> nodeModels, CaseCauseModel sys_CaseCause)
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlElement oRootNode = xmlDocument.CreateElement("案由要素");
            XmlAttribute causeNameAttr = xmlDocument.CreateAttribute("案由名称");
            causeNameAttr.Value = sys_CaseCause.CC_Name;
            oRootNode.Attributes.Append(causeNameAttr);
            XmlAttribute causeIdAttr = xmlDocument.CreateAttribute("案由ID");
            causeIdAttr.Value = sys_CaseCause.CC_ID;
            oRootNode.Attributes.Append(causeIdAttr);
            xmlDocument.AppendChild(oRootNode);
            //获取到事实要素的ID
            List<NodeModel> nodeModelChild = nodeModels.Where(no => no.ParentGuidID == sys_CaseCause.CC_ID).ToList();
            foreach (NodeModel mo in nodeModelChild)
            {
                LoopToXml(nodeModels, mo, oRootNode);
            }
            return oRootNode;

        }
        /// <summary>
        /// 进行生成xml文件
        /// </summary>
        /// <param name="nodeModels">要素所有信息</param>
        /// <param name="sys_CaseCause">案由对象</param>
        /// <returns></returns>
        public string BuildXml(List<NodeModel> nodeModels, CaseCauseModel sys_CaseCause)
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlElement oRootNode = xmlDocument.CreateElement("案由要素");
            XmlAttribute causeNameAttr = xmlDocument.CreateAttribute("案由名称");
            causeNameAttr.Value = sys_CaseCause.CC_Name;
            oRootNode.Attributes.Append(causeNameAttr);
            XmlAttribute causeIdAttr = xmlDocument.CreateAttribute("案由ID");
            causeIdAttr.Value = sys_CaseCause.CC_ID;
            oRootNode.Attributes.Append(causeIdAttr);
            xmlDocument.AppendChild(oRootNode);
            //获取到事实要素的ID
            List<NodeModel> nodeModelChild = nodeModels.Where(no => no.ParentGuidID == sys_CaseCause.CC_ID).ToList();
            foreach (NodeModel mo in nodeModelChild)
            {
                LoopToXml(nodeModels, mo, oRootNode);
            }

            return oRootNode.OuterXml;
        }

        /// <summary>
        /// 递归节点内容生成xml节点
        /// </summary>
        /// <param name="nodeModels">节点集合</param>
        /// <param name="node">节点</param>
        /// <param name="xmlNode">xml对象</param>
        private void LoopToXml(List<NodeModel> nodeModels, NodeModel node, XmlNode xmlNode)
        {
            XmlElement oRootNode = xmlNode.OwnerDocument.CreateElement(node.NodeName);
            xmlNode.AppendChild(oRootNode);

            //找到当前guid 的子节点信息包括属性信息
            List<NodeModel> TempNodes = nodeModels.Where(no => no.ParentGuidID == node.GuidID).ToList();
            //找到属性
            List<NodeModel> TempNodesAtrr = TempNodes.Where(no => no.ModelType == 2).ToList();
            XmlAttribute nAttrid = oRootNode.OwnerDocument.CreateAttribute("id");
            nAttrid.Value = node.GuidID;
            oRootNode.Attributes.Append(nAttrid);
            foreach (NodeModel atrr in TempNodesAtrr)
            {
                XmlAttribute nAttr = oRootNode.OwnerDocument.CreateAttribute(atrr.NodeName);
                if (atrr.ExtOriginalText.Count != 0)
                {
                    nAttr.Value = atrr.ExtOriginalText.Count > 0 ? atrr.ExtOriginalText[0] : "";
                    oRootNode.Attributes.Append(nAttr);
                }
            }
            //找到节点
            List<NodeModel> TempNodesNode = TempNodes.Where(no => no.ModelType == 1).ToList();
            foreach (NodeModel Tnode in TempNodesNode)
            {
                LoopToXml(nodeModels, Tnode, oRootNode);
            }
            if (oRootNode.ChildNodes.Count == 0 && oRootNode.Attributes.Count == 0)
            {
                oRootNode.ParentNode.RemoveChild(oRootNode);
            }
        }

        /// <summary>
        /// 将对象序列化成XML数据格式
        /// </summary>
        /// <typeparam name="T">对象</typeparam>
        /// <param name="obj">对象集合</param>
        /// <returns></returns>
        public string XmlSerialize<T>(T obj)
        {
            StringBuilder buffer = new StringBuilder();
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (TextWriter writer = new StringWriter(buffer))
            {
                serializer.Serialize(writer, obj);
            }
            return buffer.ToString();
        }
    }

}
